import java.util.ArrayList;
import java.util.List;

public class Solution77 {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> ans=new ArrayList<>();
        List<Integer> temp=new ArrayList<>();
        dfs(0,1,k,n,ans,temp);
        return ans;
    }

    private void dfs(int p, int last,int k, int n, List<List<Integer>> ans, List<Integer> temp) {
        if (p==k){
            ans.add(new ArrayList<>(temp));
            return;
        }
        for (int i=last;i<=n;i++){
            temp.add(i);
            dfs(p+1,i+1,k,n,ans,temp);
            temp.remove(temp.size()-1);
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution77().combine(4,2));
    }
}
